library(tidyverse)
fish <- read.csv(here::here("lab-assignments",
"lab7",
"BlackfootFish.csv")
)Challenge 7: Incorporating Multiple Inputs
Functions & Fish
This is a continuation of Lab 7: Functions & Fish.
A frequently used measurement for fish health is a “condition index.” (Wikipedia article) The following simple equation can be used to calculate the approximate condition index of a fish:
\[\text{condition index} = \frac{weight}{length^3} \times 100\]
1. There are specific units required for the calculation of a condition index. Length must be in millimeters, and weight must be in grams. Inspect the length and weight variables to decide if you believe these are the correct units associated with these measurements.
This will likely require Googling what “typical” measurements of trout are.
# Question 1 code2. Replace impossible measurements with NAs – Based on your research, write function(s) to handle the unlikely / impossible measurements included in the data set.
Your function(s) should accept three inputs
- a vector of measurements,
- the minimum value you believe is “reasonable,”
- the maximum value you believe is “reasonable.”
If a value falls outside these bounds, you should replace it with an NA.
If you are struggling with the structure of your function, I would suggest reading the Mutating Function from R4DS.
Use your function to modify the length and weight columns of the BlackfootFish data set, removing values you believe are “unreasonable.”
# Question 2 code3. Write a function which calculates the condition index of a fish, given inputs of weight and length.
Consider whether your function will accept vectors as inputs or if it will accept variable names as inputs!
# Question 3 code4. Make a thoughtful visualization of how fish conditions have varied over the duration of this study.
# Question 4 code